﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using DAL;
using Entity;
using System.Data;

namespace Project_V1
{
    public static class CommonMethods
    {
        public static List<Fastener> FastenerList = new List<Fastener>();
        public static List<Fastener> FastenerTmpList = new List<Fastener>();
        public static List<LayerStack> LayerStackList = new List<LayerStack>();
        public static List<LayerStack> LayerStackTmpList = new List<LayerStack>();


        public static List<Settings> SettingsList = new List<Settings>();

        public static DataSet SendPlcDs = new DataSet();//下发PLC参数


        public static List<Report> ReportsList = new List<Report>();//报表集合
        //通讯读取到结构体数组

        public static Report objReport = new Report();
        public static Int32 SettingsIndexBak = 0;

        //变量信息集合(主窗体Load事件初始化读取xml文件到集合)
        public static List<Variable_Ads> VarAdsList = new List<Variable_Ads>();
        //变量名和数值的字典集合(主窗体Load事件初始化,字典集合赋值<变量名,空值>)(TextBox控件使用)
        public static Dictionary<string, string> CurrentValue = new Dictionary<string, string>();
        public static Dictionary<string, string> CurrentType = new Dictionary<string, string>();
        public static Dictionary<string, string> CurrentName = new Dictionary<string, string>();

        //创建XML文件路径
        public static string VarFilePath = Application.StartupPath + "\\ConfigFile\\" + "Variable_Ads.xml";

        #region 打开窗体方法,和主界面的方法一样
        public static void OpenWindow(Form Frm, Panel MainPanel)
        {
            Frm.TopLevel = false;
            Frm.FormBorderStyle = FormBorderStyle.None;
            Frm.Dock = DockStyle.Fill;
            Frm.Parent = MainPanel;
            Frm.Show();
        }

        public static bool CloseWindow(string FrmName, Panel MainPanel)
        {
            foreach (Control ct in MainPanel.Controls)
            {
                if (ct is Form)
                {
                    Form Frm = (Form)ct;
                    if (Frm.Name == FrmName)
                    {
                        return true;
                    }
                    else
                    {
                        Frm.Close();
                    }
                }
            }
            return false;
        }
        #endregion

        #region 向XML文件中添加节点属性信息(由集合写入创建的XML文件,FrmIOVarManage中保存按钮使用)
        /// <summary>
        /// 添加节点属性信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="head"></param>
        public static void XMLAttributeAppend(XmlDocument rootxml, string name, string value, XmlElement head)
        {
            XmlAttribute att = rootxml.CreateAttribute(name);
            att.Value = value;
            head.Attributes.Append(att);
        }
        #endregion

        #region 根据节点及节点名称获取响应的值(LoadXML方法使用,读取XML配置文件到多个Variable_Modbus对象)
        /// <summary>
        /// 根据节点及节点名称获取响应的值
        /// </summary>
        /// <param name="rootxml"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string XMLAttributeGetValue(XmlNode rootxml, string name)
        {
            string resvalue = string.Empty;
            if (rootxml != null && rootxml.Attributes != null && rootxml.Attributes[name] != null)
            {
                resvalue = rootxml.Attributes[name].Value;
            }
            return resvalue;
        }
        #endregion

        #region 读取XML配置文件到Variable_Ads集合(FrmMain和FrmIOVarManage中使用,将XML文件信息存入集合)
        public static List<Variable_Ads> LoadXML(string xmlpath)
        {
            List<Variable_Ads> VarList = null;
            if (!File.Exists(xmlpath))
            {
                MessageBox.Show("IO配置变量的XML文件不存在!");
            }
            else
            {
                VarList = new List<Variable_Ads>();
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xmlpath);
                foreach (XmlNode nooderoot in xdoc.ChildNodes)
                {
                    if (nooderoot.Name == "Root")
                    {
                        foreach (XmlNode noodchild in nooderoot.ChildNodes)
                        {
                            if (noodchild.Name == "Variable")
                            {
                                Variable_Ads objVar = new Variable_Ads();
                                objVar.VarName = XMLAttributeGetValue(noodchild, "VarName");
                                objVar.DataType = XMLAttributeGetValue(noodchild, "DataType");
                                objVar.IsFiling = XMLAttributeGetValue(noodchild, "IsFiling");
                                objVar.IsAlarm = XMLAttributeGetValue(noodchild, "IsAlarm");
                                objVar.IsReport = XMLAttributeGetValue(noodchild, "IsReport");
                                objVar.Note = XMLAttributeGetValue(noodchild, "Note");
                                VarList.Add(objVar);
                            }
                        }
                    }
                }
            }
            return VarList;
        }
        #endregion

    }
}
